package com.mdlaser.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.mdlaser.annotation.CommonRest;
import com.mdlaser.api.ResponseResult;
import com.mdlaser.dto.LockedOrder;
import com.mdlaser.dto.OrderDetail;
import com.mdlaser.dto.OrdersWishesDto;
import com.mdlaser.entity.OrdersWishes;
import com.mdlaser.enums.OrderStatusType;
import com.mdlaser.service.IOrderConsumer;
import com.mdlaser.service.IOrdersWishesService;
import com.mdlaser.sys.service.IUserService;
import com.mdlaser.util.UserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mdlaser
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/orders-wishes")
@CommonRest
@Tag(name = "订单管理")
@Slf4j
public class OrdersWishesController {

    @Autowired
    IOrdersWishesService ordersWishesService;


    @Operation(summary = "获取所有订单")
    @GetMapping("/list")
    public List<OrderDetail> list() {
        return ordersWishesService.getList();
    }


    @Operation(summary = "获取我的订单")
    @GetMapping("/myOrders")
    public List<OrderDetail> myOrders() {
        return ordersWishesService.getMyOrders();
    }

    @Operation(summary = "根据订单ID获取订单详情")
    @GetMapping("/getById")
    public OrderDetail getById(@RequestParam String orderId) {
        return ordersWishesService.getOrderDetailById(orderId);
    }


    @Operation(summary = "获取不可用预约的时间段", description = "获取不可用预约的时间段")
    @GetMapping("/getDisableAppTime")
    public List<Integer> getDisableAppTime(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                           String languageId) {
        return ordersWishesService.getDisableAppTime(startTime,languageId);
    }



    @Operation(summary = "提交订单", description = "该接口返回订单详情")
    @PostMapping("/post")
    public OrderDetail post(@RequestBody OrdersWishesDto ordersWishesDto) {
        return ordersWishesService.post(ordersWishesDto);
    }

    @Operation(summary = "测试-提交订单-并支付", description = "该接口返回订单详情")
    @PostMapping("/postTest")
    public OrderDetail postTest(@RequestBody OrdersWishesDto ordersWishesDto) {
        OrderDetail post = ordersWishesService.post(ordersWishesDto);
        this.testOrder(post.getOrderId().toString());
        return post;
    }

    @Operation(summary = "删除订单")
    @GetMapping("/del")
    public ResponseResult<Object> del(@RequestParam String orderId) {
        ordersWishesService.getBaseMapper().deleteById(orderId);
        return ResponseResult.success("删除成功", null);
    }


    @Operation(summary = "修改订单")
    @PostMapping("/edit")
    public void edit(@RequestBody OrdersWishes OrdersWishes) {
        Integer orderId = OrdersWishes.getOrderId();
        OrdersWishes ordersWishes = ordersWishesService.getBaseMapper().selectById(orderId);
        if (ordersWishes == null) {
            throw new RuntimeException("订单信息不存在");
        }
        if (ordersWishes.getOrderStatus() == OrderStatusType.PAY.getId()) {
            throw new RuntimeException("无法修改支付完成的订单");
        }
        ordersWishesService.getBaseMapper().updateById(OrdersWishes);
    }

    @Operation(summary = "修改订单状态")
    @GetMapping("/changeStatus")
    public void changeStatus(@RequestParam String orderId, @RequestParam int status) {
        OrdersWishes ordersWishes = ordersWishesService.getBaseMapper().selectById(orderId);
        if (ordersWishes == null) {
            throw new RuntimeException("订单信息不存在");
        }
        if (ordersWishes.getOrderStatus() == OrderStatusType.PAY.getId()
                || status == OrderStatusType.PAY.getId()) {
            throw new RuntimeException("非法操作");
        }
        ordersWishes.setOrderStatus(status);
        ordersWishesService.getBaseMapper().updateById(ordersWishes);
    }


    @Autowired
    IUserService userService;

    @Operation(summary = "订单支付")
    @GetMapping("/createWxOrder")
    public Object createWxOrder(@RequestParam String orderId) {
        return ordersWishesService.createWxOrder(orderId);
    }


    @Operation(summary = "支付回调-微信方调用")
    @PostMapping("/paySuccess")
    @SaIgnore
    public Object paySuccess(@RequestBody String requestBody) {
        log.info("收到微信商户系统支付成功回调，回调内容:" + requestBody);
        return ordersWishesService.vxPayCallBack(requestBody);
    }


    @Operation(summary = "订单退款")
    @GetMapping("/refund")
    public Object refund(@RequestParam String orderId) {
        OrdersWishes ordersWishes = ordersWishesService.getBaseMapper().selectById(orderId);
        if (ordersWishes == null) {
            throw new RuntimeException("订单不存在，请检查！");
        }

        Date startTime = ordersWishes.getStartTime();
        long currentTime = System.currentTimeMillis();
        if (startTime != null && startTime.getTime() - currentTime < 1000 * 60 * 30) {
            throw new RuntimeException("距离播放时间还有三十分钟，无法退款");
        }
        return ordersWishesService.refund(orderId,"用户主动退款");
    }

    @Operation(summary = "退款回调-微信方调用")
    @PostMapping("/refundSuccess")
    @SaIgnore
    public Object refundSuccess(@RequestBody String requestBody) {
        log.info("收到微信商户系统退款成功回调，回调内容:" + requestBody);
        return ordersWishesService.vxRefundSuccess(requestBody);
    }


    @Autowired
    IOrderConsumer orderConsumer;

    @Operation(summary = "测试-模拟订单支付成功", description = "测试-模拟订单支付成功")
    @PostMapping("/testOrder")

    public String testOrder(@RequestParam String orderId) {
        OrdersWishes ordersWishes = ordersWishesService.getBaseMapper().selectById(orderId);
        if (ordersWishes == null) {
            throw new RuntimeException("订单不存在！");
        }
        ordersWishesService.createPayOrder("1234",orderId);
        return "success";
    }




}
